/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ses;

import Entity.Feedback;
import ses.exceptions.NonexistentEntityException;
import ses.exceptions.PreexistingEntityException;
import ses.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entity.FeedbackType;
import Entity.Accounts;
import javax.transaction.UserTransaction;

/**
 *
 * @author Huu Vinh
 */
public class FeedbackJpaController implements Serializable {

    public FeedbackJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Feedback feedback) throws PreexistingEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            FeedbackType FBTypeId = feedback.getFBTypeId();
            if (FBTypeId != null) {
                FBTypeId = em.getReference(FBTypeId.getClass(), FBTypeId.getFBTypeId());
                feedback.setFBTypeId(FBTypeId);
            }
            Accounts accountId = feedback.getAccountId();
            if (accountId != null) {
                accountId = em.getReference(accountId.getClass(), accountId.getAccId());
                feedback.setAccountId(accountId);
            }
            em.persist(feedback);
            if (FBTypeId != null) {
                FBTypeId.getFeedbackList().add(feedback);
                FBTypeId = em.merge(FBTypeId);
            }
            if (accountId != null) {
                accountId.getFeedbackList().add(feedback);
                accountId = em.merge(accountId);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findFeedback(feedback.getFBId()) != null) {
                throw new PreexistingEntityException("Feedback " + feedback + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Feedback feedback) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Feedback persistentFeedback = em.find(Feedback.class, feedback.getFBId());
            FeedbackType FBTypeIdOld = persistentFeedback.getFBTypeId();
            FeedbackType FBTypeIdNew = feedback.getFBTypeId();
            Accounts accountIdOld = persistentFeedback.getAccountId();
            Accounts accountIdNew = feedback.getAccountId();
            if (FBTypeIdNew != null) {
                FBTypeIdNew = em.getReference(FBTypeIdNew.getClass(), FBTypeIdNew.getFBTypeId());
                feedback.setFBTypeId(FBTypeIdNew);
            }
            if (accountIdNew != null) {
                accountIdNew = em.getReference(accountIdNew.getClass(), accountIdNew.getAccId());
                feedback.setAccountId(accountIdNew);
            }
            feedback = em.merge(feedback);
            if (FBTypeIdOld != null && !FBTypeIdOld.equals(FBTypeIdNew)) {
                FBTypeIdOld.getFeedbackList().remove(feedback);
                FBTypeIdOld = em.merge(FBTypeIdOld);
            }
            if (FBTypeIdNew != null && !FBTypeIdNew.equals(FBTypeIdOld)) {
                FBTypeIdNew.getFeedbackList().add(feedback);
                FBTypeIdNew = em.merge(FBTypeIdNew);
            }
            if (accountIdOld != null && !accountIdOld.equals(accountIdNew)) {
                accountIdOld.getFeedbackList().remove(feedback);
                accountIdOld = em.merge(accountIdOld);
            }
            if (accountIdNew != null && !accountIdNew.equals(accountIdOld)) {
                accountIdNew.getFeedbackList().add(feedback);
                accountIdNew = em.merge(accountIdNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = feedback.getFBId();
                if (findFeedback(id) == null) {
                    throw new NonexistentEntityException("The feedback with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Feedback feedback;
            try {
                feedback = em.getReference(Feedback.class, id);
                feedback.getFBId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The feedback with id " + id + " no longer exists.", enfe);
            }
            FeedbackType FBTypeId = feedback.getFBTypeId();
            if (FBTypeId != null) {
                FBTypeId.getFeedbackList().remove(feedback);
                FBTypeId = em.merge(FBTypeId);
            }
            Accounts accountId = feedback.getAccountId();
            if (accountId != null) {
                accountId.getFeedbackList().remove(feedback);
                accountId = em.merge(accountId);
            }
            em.remove(feedback);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Feedback> findFeedbackEntities() {
        return findFeedbackEntities(true, -1, -1);
    }

    public List<Feedback> findFeedbackEntities(int maxResults, int firstResult) {
        return findFeedbackEntities(false, maxResults, firstResult);
    }

    private List<Feedback> findFeedbackEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Feedback as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Feedback findFeedback(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Feedback.class, id);
        } finally {
            em.close();
        }
    }

    public int getFeedbackCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Feedback as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
